package boun.swe.bob.service;

import boun.swe.bob.entities.Bob_Event;
import boun.swe.bob.entities.Group;
import boun.swe.bob.entities.User;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * This controller is used for search page actions
 */
public class SearchController extends BaseController{

        /**
     * Shows the page
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return page
     */
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();

        String searchWord = httpServletRequest.getParameter("search");
        List<User> searchResultList = new ArrayList<User>();
        List<Group> searchResultListGroups = new ArrayList<Group>();
        List<Bob_Event> searchResultListEvents = new ArrayList<Bob_Event>();

        String group = httpServletRequest.getParameter("group1");
         if("friends".equals(group)) {
            if(!Strings.isNullOrEmpty(searchWord)) {
                searchResultList = userDao.findUsersForSearch(searchWord);
            }
            myModel.put("resultList", searchResultList);
        }
        if("groups".equals(group)) {
            if(!Strings.isNullOrEmpty(searchWord)) {
                searchResultListGroups = groupDao.findGroupsForSearch(searchWord);
            }
            myModel.put("resultListGroup", searchResultListGroups);
        }
        if("events".equals(group)) {
            if(!Strings.isNullOrEmpty(searchWord)) {
                searchResultListEvents = bob_EventDao.findEventsForSearch(searchWord);
            }
            myModel.put("resultListEvent", searchResultListEvents);
        }
        myModel.put("himself", true);
        myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));               

        return new ModelAndView("search", "model", myModel);
    }


}
